package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.search.vos.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Service
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {
    @Autowired
    private ApArticleContentMapper contentMapper;

    @Autowired
    private Configuration configuration;

    @Autowired
    FileStorageService fileStorageService;

    @Autowired
    ApArticleMapper apArticleMapper;

    /**
     * 生成静态文件上传到minIO中
     *
     * @param articleId
     */
    @Override
    @Async
    public void buildArticleToMinIO(Long articleId) throws Exception {
        //1.根据文章的id查询文章的内容
        ApArticleContent articleContent = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, 1390536764510310401L));
        //2.获取freemarker模板对象，并生成一个静态的html
        Template template = configuration.getTemplate("article.ftl");
        StringWriter out = new StringWriter();

        Map<String, Object> params = new HashMap<>();
        params.put("content", JSONArray.parseArray(articleContent.getContent()));

        template.process(params, out);
        //3.把html上传到minio中并返回访问的地址
        InputStream inputStream = new ByteArrayInputStream(out.toString().getBytes());

        String path = fileStorageService.uploadHtmlFile("", articleContent.getArticleId() + ".html", inputStream);

        //4.把返回的地址更新到ap_article表中
        ApArticle apArticle = new ApArticle();
        apArticle.setId(apArticle.getId());
        apArticle.setStaticUrl(path);
        apArticleMapper.updateById(apArticle);

        //发送消息，同步es索引数据
        createArticleESIndex(apArticle, articleContent.getContent(),path);


    }
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    /**
     * 发送消息  同步es索引数据
     * @param apArticle
     * @param content
     */
    private void createArticleESIndex(ApArticle apArticle, String content,String path) {
        SearchArticleVo searchArticleVo = new SearchArticleVo();
        BeanUtils.copyProperties(apArticle,searchArticleVo);

        searchArticleVo.setContent(content);
        searchArticleVo.setStaticUrl(path);
        searchArticleVo.setId(apArticle.getId() + "");

        //发送消息
        kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(searchArticleVo));

    }
}
